package cn.edu.ctbu.myfirstweb.service.impl;

import cn.edu.ctbu.myfirstweb.dao.CourseRepository;
import cn.edu.ctbu.myfirstweb.dao.StudentRepository;
import cn.edu.ctbu.myfirstweb.dao.StudentScoreEntityRepository;
import cn.edu.ctbu.myfirstweb.domain.Course;
import cn.edu.ctbu.myfirstweb.domain.Student;
import cn.edu.ctbu.myfirstweb.domain.StudentScore;
import cn.edu.ctbu.myfirstweb.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class StudentImplService implements StudentService {
    @Autowired//依赖注入
    StudentRepository studentRepository;//提供学生的基础数据操做

    @Autowired
    StudentScoreEntityRepository studentScoreEntityRepository;//提供分数查询

    @Autowired
    CourseRepository courseRepository;//提供可选课程查询

    public List<Course> getThisSemesterCourse(Long id) {

        return courseRepository.getThisSemesterCourse(id);
    }

    public List<Course> getStudentCourse(Long id) {

        return courseRepository.getCourse(id);
    }
    /**
     * 学生用户课程成绩查询函数
     *
     * @param id
     * @return
     */
    public List<StudentScore> getStudentScore(Long id) {

        return studentScoreEntityRepository.getScore(id);
    }

    /***
     * 登录验证函数
     * @param student
     * @return
     */
    public boolean login(Student student) {
        Long studentNum = student.getNum();
        String studentPassword = student.getPassword();
        Student student1 = studentRepository.findByNum(studentNum);
        if (student1 == null) {
            return false;
        } else {
            if (student1.getPassword().equals(studentPassword)) {
                return true;
            } else {
                return false;
            }
        }
    }

    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    public Student getByNum(Long num) {
        return studentRepository.findByNum(num);
    }

    /**
     * 分页显示全部数据
     *
     * @param pageable
     * @return
     */

    public Page<Student> findAll(Pageable pageable) {
        return studentRepository.findAll(pageable);
    }

    /**
     * 按例查询
     *
     * @param student
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Example<Student> student, Pageable pageable) {
        return studentRepository.findAll(student, pageable);
    }

    /**
     * 插入学生
     *
     * @param student
     * @return
     */
    public Student insert(Student student) {

        return studentRepository.save(student);
    }

    /**
     * @param student
     * @return cn.edu.ctbu.myfirstweb.domain.Student
     * @description
     * @author HighJ
     * @date 2021/11/3 23:22
     */

    public Student update(Student student) {

        return studentRepository.save(student);
    }

    /**
     * @param id
     * @return cn.edu.ctbu.myfirstweb.domain.Student
     * @description
     * @author HighJ
     * @date 2021/11/3 23:19
     */

    public Student query(Long id) {
        Optional<Student> stu = studentRepository.findById(id);
        return stu.orElseThrow(() -> new RuntimeException("No such student found"));
    }

    /**
     * 根据名字查找学生
     *
     * @param name
     * @return
     */
    public Student findByName(String name) {
        return studentRepository.findByName(name);
    }

    /**
     * 根据名字删除
     *
     * @param name
     */
    public void deleteByName(String name) {
        studentRepository.deleteByName(name);
    }

    /**
     * 根据名字删除
     *
     * @param id
     */
    public void deleteById(Long id) {
        studentRepository.deleteById(id);
    }

    /**
     * 根据id获取学生
     *
     * @param id
     * @return
     */
    public Student getById(Long id) {
        return studentRepository.findById(id).orElse(null);
    }

    /**
     * 根据名字获取学生
     *
     * @param name
     * @return
     */
    public Student getByName(String name) {
        return studentRepository.findByName(name);
    }

    public Student login(Long num, String password) {
        return studentRepository.findByNumAndPassword(num,password); }

}
